package com.qire.manhua.model.vo;

import com.qire.common.collect.SafeIterableMap;
import com.qire.common.collect.SafeIterableList;
import com.qire.common.support.base.BaseVO;
import com.qire.manhua.model.entity.BulletScreenInfo;
import com.qire.manhua.model.entity.ComicBookEntity.ChaptersInfo;

import java.math.BigDecimal;

public class ChaptersBulletScreenVO extends BaseVO {

    public int chapterId;               //: "19261",
    public int bookId;                  //: "1003",
    public int imgsCount;               //: 37,
    public int roastsCount;             //: 0,
    public BulletScreenItemVO[] roasts; //: [BulletScreenItemVO]

    public class BulletScreenItemVO{
        public int id;                  //: 1945,
        public String content;          //: "😄不能自拔😄😄🌹😄😄？？@",
        public String color;            //: "#ffffff",
        public int font_size;           //: 36,
        public String xpos;             //: "0.65066667",
        public String ypos;             //: "0.24736472",
        public String img;              //: "5b1a294052892",
        public String url;              //: "https://p.qiremanhua.com/uploads/chapter/10064/201806/5b1a294052892.jpg",
        public int imgPx;               //: 0

        private BulletScreenInfo to() {
            BigDecimal xRatio  = new BigDecimal(xpos);
            BigDecimal yRatio  = new BigDecimal(ypos);
            int fontSizeNumber = (1 <= font_size && font_size <= 3) ? font_size : 2;

            String fontColorTag        = color;
            String contentPictureToken = img;
            String contentPictureUrl   = url;

            return new BulletScreenInfo(id, content, fontSizeNumber, fontColorTag, xRatio, yRatio, contentPictureToken, contentPictureUrl, imgPx);
        }
    }

    public void fillComicBulletScreen(ChaptersInfo chaptersInfo) {
        if(roasts == null) {
            return;
        }

        chaptersInfo.clearBulletScreen();
        SafeIterableMap<String,SafeIterableList<BulletScreenInfo>> bulletScreenInfoMap = chaptersInfo.bulletScreenInfoMap();
        for(BulletScreenItemVO itemVO : roasts) {

            BulletScreenInfo bulletScreenInfo = itemVO.to();

            SafeIterableList<BulletScreenInfo> list = bulletScreenInfoMap.getValue(itemVO.img);
            if(list != null) {
                list.add(bulletScreenInfo);
            } else {
                list = new SafeIterableList<>();
                list.add(bulletScreenInfo);
                bulletScreenInfoMap.putIfAbsent(itemVO.img, list);
            }
        }
    }

}
